package com.xframework.dao;

import com.xframework.Base;
import com.xframework.model.BaseModel;
import com.xframework.model.SqlParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public abstract class BaseDao extends Base {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    private XJdbcTemplate xJdbcTemplate;

    public BaseDao() {
    }

    @Autowired
    public void setXJdbcTemplate(XJdbcTemplate xJdbcTemplate) {
        this.xJdbcTemplate = xJdbcTemplate;
    }

    public int getSeqCurrval(String seqName) throws Exception {
        return this.xJdbcTemplate.getSeqCurrval(seqName);
    }

    public int getSeqNextval(String seqName) throws Exception {
        return this.xJdbcTemplate.queryForInt(seqName);
    }

    public int queryForInt(SqlParams sqlParams) throws Exception {
        return this.xJdbcTemplate.queryForInt(sqlParams);
    }

    public int queryForInt(String sql, List params) throws Exception {
        return this.xJdbcTemplate.queryForInt(sql, params);
    }

    public int queryForInt(String sql, Object... params) throws Exception {
        return this.xJdbcTemplate.queryForInt(sql, params);
    }

    public <T extends BaseModel> T queryForObject(SqlParams sqlParams) throws Exception {
        return this.xJdbcTemplate.queryForObject(sqlParams);
    }

    public <T extends BaseModel> T queryForObject(SqlParams sqlParams, Class<T> clazz) throws Exception {
        return this.xJdbcTemplate.queryForObject(sqlParams, clazz);
    }

    public <T extends BaseModel> T queryForObject(String sql, Class<T> clazz, Object... params) throws Exception {
        return this.xJdbcTemplate.queryForObject(sql, clazz, params);
    }

    public <T extends BaseModel> T queryForObject(String sql, Class<T> clazz, List params) throws Exception {
        return this.xJdbcTemplate.queryForObject(sql, clazz, params);
    }

    public <T> T queryForObject(String sql, RowMapper<T> mapper, Object... params) throws Exception {
        return this.xJdbcTemplate.queryForObject(sql, mapper, params);
    }

    public <T> T queryForObject(String sql, RowMapper<T> mapper, List params) throws Exception {
        return this.xJdbcTemplate.queryForObject(sql, mapper, params);
    }

    /**
     * 数组参数查询返回list
     *
     * @return List
     */
    public <T extends BaseModel> List<T> queryForList(SqlParams sqlParams) throws Exception {
        return this.xJdbcTemplate.queryForList(sqlParams);
    }

    public <T extends BaseModel> List<T> queryForList(SqlParams sqlParams, Class<T> clazz) throws Exception {
        return this.xJdbcTemplate.queryForList(sqlParams, clazz);
    }

    public <T extends BaseModel> List<T> queryForList(String sql, Class<T> clazz, BaseModel model, Object... params) throws Exception {
        return this.xJdbcTemplate.queryForList(sql, clazz, model, params);
    }

    public <T extends BaseModel> List<T> queryForList(String sql, Class<T> clazz, BaseModel model, List params) throws Exception {
        return this.xJdbcTemplate.queryForList(sql, clazz, model, params);
    }

    public <T> List<T> queryForList(String sql, RowMapper<T> mapper, BaseModel model, Object... params) throws Exception {
        return this.xJdbcTemplate.queryForList(sql, mapper, model, params);
    }

    public <T> List<T> queryForList(String sql, RowMapper<T> mapper, BaseModel model, List params) throws Exception {
        return this.xJdbcTemplate.queryForList(sql, mapper, model, params);
    }

    /**
     * 数组参数更新操作
     */
    public <T extends BaseModel> T insert(T model) throws Exception {
        return this.xJdbcTemplate.insert(model);
    }

    public <T extends BaseModel> int update(T model) throws Exception {
        return this.xJdbcTemplate.update(model);
    }

    public <T extends BaseModel> int update(T modelSet, T modelWhere) throws Exception {
        return this.xJdbcTemplate.update(modelSet, modelWhere);
    }

    public <T extends BaseModel> int delete(T model) throws Exception {
        return this.xJdbcTemplate.delete(model);
    }

    public int execute(String sql, Object... params) throws Exception {
        return this.xJdbcTemplate.execute(sql, params);
    }

    public int execute(String sql, List params) throws Exception {
        return this.xJdbcTemplate.execute(sql, params);
    }

    /**
     * 调用存储过程
     *
     * @param sql      String
     * @param inParams Object[]
     * @param inTypes  int[]
     * @param outTypes int[]
     * @return Object[]
     */
    public Object[] callableStatement(final String sql, final Object[] inParams, final int[] inTypes, final int[] outTypes) throws Exception {
        return this.xJdbcTemplate.callableStatement(sql, inParams, inTypes, outTypes);
    }

}
